package config;

import lombok.Data;

import java.util.Properties;

/**
 * 数据库连接池配置
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Data
public class DBPoolConfig {

    public final static int DEFAULT_INITIAL_SIZE = 1;
    public final static int DEFAULT_MAX_ACTIVE_SIZE = 8;
    public final static int DEFAULT_MAX_IDLE = 8;
    public final static int DEFAULT_MIN_IDLE = 0;
    public final static int DEFAULT_MAX_WAIT = -1;

    /**
     * 数据库jdbc属性
     */
    private String driverName;  //数据库的驱动类
    private String url;         //数据库的连接地址
    private String userName;    //数据库用户名
    private String password;    //数据库密码

    /**
     * 连接池配置
     */
    private int minIdle = DEFAULT_MIN_IDLE;            //空闲集合中最少连接数
    private int maxIdle = DEFAULT_MAX_IDLE;            //空闲集合最多的连接数
    private int initConn = DEFAULT_INITIAL_SIZE;            //初始连接数
    private int maxActiveConn = DEFAULT_MAX_ACTIVE_SIZE;     //整个连接池（数据库）允许的最大连接数
    private int waitTime = DEFAULT_MAX_WAIT;                //单位毫秒，连接数不够时，线程等待的时间
    private boolean isCheck = false;    //数据库连接池是否启用自检机制（间隔一段时间检测连接池状态）
    private long checkPeriod = 1000*30; //自检周期

    public DBPoolConfig(Properties prop) {
        //获取配置文件信息传入config连接池配置对象
        this.driverName = prop.getProperty("jdbc.driverName");
        this.url = prop.getProperty("jdbc.url");
        this.userName = prop.getProperty("jdbc.userName");
        this.password = prop.getProperty("jdbc.password");
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DBPoolConfig{");
        sb.append("driverName='").append(driverName).append('\'');
        sb.append(", url='").append(url).append('\'');
        sb.append(", userName='").append(userName).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", minIdle=").append(minIdle);
        sb.append(", maxIdle=").append(maxIdle);
        sb.append(", initConn=").append(initConn);
        sb.append(", maxActiveConn=").append(maxActiveConn);
        sb.append(", waitTime=").append(waitTime);
        sb.append(", isCheck=").append(isCheck);
        sb.append(", checkPeriod=").append(checkPeriod);
        sb.append('}');
        return sb.toString();
    }
}
